package com.hy.data.integrate;

import com.hy.data.integrate.mapper.ProjectApprovalMapper;
import com.hy.data.integrate.server.impl.ProjectNewService;
import com.hy.data.integrate.server.SendStampService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Scanner;

/**
 * 控制台
 * 启动配置 spring.profiles.active: dev,cmd
 */
@Profile("cmd")
@Component
public class Command implements CommandLineRunner {

    @Autowired
    private ProjectApprovalMapper projectApprovalMapper;
    @Autowired
    private SendStampService sendStampService;
    @Autowired
    private ProjectNewService projectNewService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("命令行启动");
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.print("cmd>");

            String[] cmd;
            while (scanner.hasNextLine()) {
                try {
                    cmd = scanner.nextLine().split(" ");
                    switch (cmd[0]) {
                        case "failResend": {
                            projectNewService.failResend();
                        }
                        break;
                        case "push": {
                            projectNewService.push();
                        }
                        break;
                        case "get": {
                            projectNewService.getProjectType(cmd[1]);
                        }
                        break;
                        case "run": {
                            projectNewService.run();
                        }
                        break;
                        case "exit": {
                            System.exit(0);
                        }
                        break;
                        default: {
                            System.out.println("\t unknown command [" + cmd[0] + "]");
                        }
                    }
                    System.out.print("cmd>");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
